import type { RouteRecordRaw } from "vue-router"
import { createRouter } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/register",
    component: () => import("@/pages/login/register.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/forget",
    component: () => import("@/pages/login/forgetpassword.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/codeLogin",
    component: () => import("@/pages/login/indexByCode.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/wechatlogin",
    component: () => import("@/pages/login/wechatlogin.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/wechatloginiframe",
    component: () => import("@/pages/login/wechatloginiframe.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/wechatloginsuccess",
    component: () => import("@/pages/login/wechatloginsuccess.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/info",
    component: () => import("@/pages/index/info.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/swiper",
    component: () => import("@/pages/index/swiper0.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/detail",
    name: "newsDetail2",
    component: () => import("@/pages/index/detail.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/detail/:id",
    name: "newsDetail",
    component: () => import("@/pages/index/detail.vue"),
    meta: {
      hidden: true
    }
  },
  // {
  //   path: "/details",
  //   name: "/newsDetails",
  //   component: () => import("@/pages/index/detail.vue"),
  //   meta: {
  //     hidden: true
  //   }
  // },
  {
    path: "/ai",
    component: () => import("@/pages/ai/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai2",
    component: () => import("@/pages/ai/index2.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai8/:id",
    name: "aiAsk",
    component: () => import("@/pages/ai/index2.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai3",
    component: () => import("@/pages/ai/yaosukaList.vue"),
    meta: {
      hidden: true
    }
  },
  // 要素卡详情页
  {
    path: "/ai-detail/:id",
    name: "yasukaDetail",
    component: () => import("@/pages/ai/yaosukaDetail.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai4",
    component: () => import("@/pages/ai/index4.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai5",
    component: () => import("@/pages/ai/index5.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/study2",
    component: () => import("@/pages/study/index1.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/ai-original",
    component: () => import("@/pages/ai/original.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/study-second",
    component: () => import("@/pages/study/study-second.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/study-second/:id",
    name: "studySecond",
    component: () => import("@/pages/study/study-second.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/study",
    component: () => import("@/pages/study/index2.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/PMS",
    component: () => import("@/pages/PMS/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/index",
    component: () => import("@/pages/index/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/friend",
    component: () => import("@/pages/study/friend-list.vue"),
    meta: {
      hidden: true
    }
  },
  {
    name: "frienddetail",
    path: "/frienddetail/:id",
    component: () => import("@/pages/study/friend-detail.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/quanyi",
    component: () => import("@/pages/index/quanyi.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/index",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/index/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page-level",
    name: "Permission",
    meta: {
      title: "权限演示",
      elIcon: "Lock",
      // 可以在根路由中设置角色
      roles: ["admin", "editor"],
      alwaysShow: true
    },
    children: [
      {
        path: "page-level",
        component: () => import("@/pages/demo/permission/page-level.vue"),
        name: "PermissionPageLevel",
        meta: {
          title: "页面级",
          // 或者在子路由中设置角色
          roles: ["admin"]
        }
      },
    ]
  }
]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
